<?php

class Peserta_con extends CI_Controller {

    function index() {
        $this->tampil_peserta();
    }

    function round_up($x,$y){
        $m=$x/$y;
        if($x%$y>0){
            return $m+1;
        }else{
            return $m;
        }
    }
    
    function tampil_peserta($page=1) {
        $opset = 20*($page-1);
        $strjum = "SELECT DISTINCT count(ID) as jum FROM role natural join daftar_role where NAMA_ROLE like 'Peserta'";
        $jumpage = $this->db->query($strjum)->row()->jum;
        $jumpage = $this->round_up($jumpage, 20);
        $strquery = "SELECT DISTINCT ID,NAMA FROM user LIMIT 20 OFFSET ".$opset;
        $query = $this->db->query($strquery);
        if ($query->num_rows() > 0) {
            $this->load->library('table');
            $this->table->set_template(array('table_open' => '<table align="center" width="40%" border="1" cellpadding="1" cellspacing="1">'));
            $this->table->set_heading('NO','ID', 'Nama', 'Aksi');
            $i = 20*($page-1)+1;
            foreach ($query->result() as $entry) {
                $strquery = "SELECT * FROM role natural join daftar_role where ID=" . $entry->ID . " AND NAMA_ROLE NOT LIKE 'Peserta'";
                $query = $this->db->query($strquery);
                if ($query->num_rows() == 0) {
                    $this->table->add_row(
                            $i, $entry->ID,anchor(base_url() . 'admin/peserta_con/view_peserta/' . $entry->ID, $entry->NAMA), anchor(base_url() . 'admin/peserta_con/edit_peserta/' . $entry->ID, 'EDIT')
                            . ' | ' .
                            anchor(base_url() . 'admin/peserta_con/delete/' . $entry->ID, 'DELETE'));
                    $i++;
                }
            }
            $display['content'] = $this->table->generate();
        } else {
            $display['content'] = 'Belum ada data';
        }
        $display['page']=$page;
        $display['jumpage']=$jumpage;
        $data['title'] = "List User";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/list_peserta', $display);
        $this->load->view('footer_view');
    }

    function view_peserta($id) {
        
    }

    function tambah_peserta() {
        
    }

    function upload_peserta() {
        $data['title'] = "Upload peserta";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/upload_peserta', array('error' => ''));
        $this->load->view('footer_view');
    }

    function do_upload() {
        $config['upload_path'] = './public/';
        $config['allowed_types'] = 'csv';
        $this->load->library('upload', $config);

        $strquery = "select ID_ROLE from daftar_role where NAMA_ROLE like 'Peserta'";
        $idrole = $this->db->query($strquery)->row()->ID_ROLE;

        if (!$this->upload->do_upload('data')) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/upload_peserta', $error);
            $this->load->view('footer_view');
        } else {

            $data = array('upload_data' => $this->upload->data());
            $namafile = $data['upload_data']['full_path'];
            $row = 1;
            if (($handle = fopen($namafile, "r")) !== FALSE) {
                $this->load->library('table');
                $this->table->set_template(array('table_open' => '<table width="50%" border="1" cellpadding="4" cellspacing="3">'));
                $this->table->set_heading('No', 'ID', 'Nama', 'No CIC');
                $i = 0;
                while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
                    $num = count($data);
                    if ($num != 3) {
                        $this->table->add_row('Gagal', 'Gagal', 'Gagal', 'Gagal');
                    } else {
                        $id = $data[0];
                        $nama = $data[1];
                        $nocic = $data[2];
                        $username = str_ireplace(' ', '.', strtolower($nama));
                        $password = md5($username);
                        $user = array('ID' => $id, 'NAMA' => $nama,'USERNAME'=>$username,'PASSWORD'=>$password);
                        $this->db->insert('user', $user);
                        $peserta = array('ID' => $id, 'NO_CIC_CARD' => $nocic, 'STATUS_CIC_CARD'=>TRUE);
                        $this->db->insert('peserta', $peserta);
                        $role = array('ID' => $id, 'ID_ROLE' => $idrole);
                        $this->db->insert('role', $role);
                    }
                }
                fclose($handle);
            }
            redirect(base_url().'admin/peserta_con');
        }
    }

    function edit_peserta($id) {
        
    }

    function update() {
        
    }

    function delete_peserta($id) {
        
    }

    function delete() {
        
    }

}

?>
